package com.xiaozhichao.javaapi.designpattern.observer.instance.listener;

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ApplicationContext();

        context.addApplicationListener(new ApplicationListener() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                Object source = event.getSource();
                if(source instanceof Integer){
                    int now = (int) source;
                    System.out.println("检测到事件源为整型：事件源变为" + now);
                }
            }
        });

        /**
         * 添加监听事件源为字符串类型的监听器
         */
        context.addApplicationListener(new ApplicationListener() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                Object source = event.getSource();
                if (source instanceof String) {
                    String now = (String) source;
                    System.out.println("检测到事件源为字符串类型：事件源变为" + now);
                }
            }
        });

        context.publishEvent(new ApplicationEvent("hello"));

    }
}
